<template>
	<div>
		<form>
			<el-row>
				<el-col :span="10">
					<el-input v-model="registerId" placeholder="请输入病历号"></el-input>
				</el-col>

				<el-col :span="4">
					<el-button type="primary" @click="list">查询</el-button>
				</el-col>
			</el-row>
		</form>


		<el-tag style="width: 300px;margin-top: 20px;margin-right: 15px;">已申请的检查、检验项目</el-tag>
		
		<el-table  ref="tab" :data="tableData" size="mini" style="width: 100%" @select="select">


			<el-table-column type="selection">

			</el-table-column>

			<el-table-column label="病历号" prop="registerId">
			</el-table-column>

			<el-table-column label="患者名称" prop="name">
			</el-table-column>

			<el-table-column label="项目名称" prop="itemName">
			</el-table-column>

			<el-table-column label="单价" prop="fee">
			</el-table-column>

			<el-table-column label="开立时间" prop="createtime">
			</el-table-column>

			<el-table-column label="挂号状态">
				<template slot-scope="scope">

					<el-tag v-if="scope.row.status == 1">待缴费</el-tag>
					<el-tag type="success" v-else-if="scope.row.status == 2">待检查</el-tag>
					<el-tag type="info" v-else-if="scope.row.status == 3">已检查</el-tag>
					<el-tag type="danger" v-else-if="scope.row.status == 4">已退费</el-tag>

				</template>
			</el-table-column>


		</el-table>
		<el-button type="primary" size="mini" @click="refund">退费</el-button> <el-tag type="success">应退费用: {{calFee}} 元</el-tag>

	</div>
</template>

<script>
	export default {
		name: 'Refund',
		components: {

		},
		data() {
			return {
				registerId: '',
				tableData: [],
				selectionRow:[]
			}
		},
		methods: {
			list() {

				//&=2
				this.axios.get('register?type=queryFeeItemList', (data) => {
					this.tableData = data
				}, {
					registerId: this.registerId
				})
			},
			
			//selection 所有选中行的数据
			//row 当前点击选中的数据
			select(selection, row){
				
				if(row.status !== 2){
					//找到表格，取消选中
					this.$refs.tab.toggleRowSelection(row,false);
					
					for (var i = 0; i < selection.length; i++) {
						if(selection[i].itemId == row.itemId && 
							selection[i].feeType == row.feeType ){
							selection.splice(i,1);
							break;
						}
					}
				}

				this.selectionRow = selection;
			},
			//申请检查的id
			checckApplyId(){
				let ids = [];
				for (var i = 0; i < this.selectionRow.length; i++) {
					if(this.selectionRow[i].feeType == 1){ //检查
						ids.push(this.selectionRow[i].id)
					}
				}
				return ids;
			},
			//申请检验的id
			inspectApplyId(){
				let ids = [];
				for (var i = 0; i < this.selectionRow.length; i++) {
					if(this.selectionRow[i].feeType == 2){ //检验
						ids.push(this.selectionRow[i].id)
					}
				}
				return ids;
			},
			refund(){
				
				this.axios.get('/register?type=fee',success=>{
					this.$message.success("退费成功【"+this.calFee+"】")
					this.list();
					this.selectionRow = [];
				},{
					dealType:'refund',
					checckApplyIds:this.checckApplyId().join(","),
					inspectApplyIds:this.inspectApplyId().join(",")
				})
				
				
			}
		},
		computed:{
			calFee(){
				let fee = 0;
				for (var i = 0; i < this.selectionRow.length; i++) {
					fee += parseFloat(this.selectionRow[i].fee)
				}
				return fee;
			}
			
		},
		created() {
			//this.list();
		}
	}
</script>

<style>
</style>
